package com.databasedictionary.controller;

import com.databasedictionary.entity.TableInfo;
import com.databasedictionary.service.MakeDownInfo;
import com.databasedictionary.service.MysqlDatabase;
import com.databasedictionary.service.OracleDatabase;
import com.databasedictionary.utils.*;
import org.apache.catalina.filters.ExpiresFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class GeneratorController {

    // 根据命名规范，只修改此常量值即可
    private static String MODULE = "";
    private static String proertiesName = "generator-oracle";//配置文件名
    private static String DATABASE = "club_sale"; // ORACLE不需要
    private static String JDBC_DRIVER = PropertiesFileUtil.getInstance(proertiesName).get("generator.jdbc.driver");
    private static String JDBC_URL = PropertiesFileUtil.getInstance(proertiesName).get("generator.jdbc.url");
    private static String JDBC_USERNAME = PropertiesFileUtil.getInstance(proertiesName).get("generator.jdbc.username");
    private static String JDBC_PASSWORD = PropertiesFileUtil.getInstance(proertiesName).get("generator.jdbc.password");
    // 需要insert后返回主键的表配置，key:表名,value:主键名
    private static Map<String, String> LAST_INSERT_ID_TABLES = new HashMap<>();

    @RequestMapping("/generator")
    public String generator() {
            return "generator";
    }

    @RequestMapping("/getGenerator")
    public String getGenerator(String TABLE_PREFIX,String PACKAGE_NAME, HttpServletResponse response) {
        try {
            if(StringUtil.isEmpty(TABLE_PREFIX) || TABLE_PREFIX.length()<3){
                return "error";
            }
            String toZIP = MybatisGeneratorForOracleUtil.generatorToZIP(JDBC_DRIVER, JDBC_URL, JDBC_USERNAME, JDBC_PASSWORD, MODULE, DATABASE, TABLE_PREFIX, PACKAGE_NAME, LAST_INSERT_ID_TABLES);
            ZIPUtil.craeteZipPath(toZIP,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
//    @RequestMapping("/getGenerator")
//    public void getGenerator(String IP,String PORT,String SERVER_NAME, String JDBC_USERNAME, String JDBC_PASSWORD, String TABLE_PREFIX,String PACKAGE_NAME, HttpServletResponse response) {
//        try {
//            String JDBC_URL = "jdbc:oracle:thin:@//"+IP+":"+PORT+"/"+SERVER_NAME;
//            String toZIP = MybatisGeneratorForOracleUtil.generatorToZIP(JDBC_DRIVER, JDBC_URL, JDBC_USERNAME, JDBC_PASSWORD, MODULE, DATABASE, TABLE_PREFIX, PACKAGE_NAME, LAST_INSERT_ID_TABLES);
//            ZIPUtil.craeteZipPath(toZIP,response);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
}